package com.mediacloud.app.newsmodule.fragment.baoliao.adapter;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mediacloud.app.assembly.util.DrawableUtils;
import com.mediacloud.app.assembly.views.NetImageViewX;
import com.mediacloud.app.model.news.NewBaoLiaoListModel;
import com.mediacloud.app.model.utils.DefaultBgUtil;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.fragment.baoliao.OnItemClickListener;

import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;
import com.mediacloud.app.reslib.enums.ListStyle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Yoake on 2018/8/24.
 */

public class MyBaoLiaoListAdapter extends RecyclerView.Adapter<MyBaoLiaoListAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<NewBaoLiaoListModel.DataBean.PageRecordsBean> list;
    private OnItemClickListener listener;

    public MyBaoLiaoListAdapter(Context context, OnItemClickListener listener) {
        this.context = context;
        this.listener = listener;
        list = new ArrayList<>();
    }

    public void setList(List<NewBaoLiaoListModel.DataBean.PageRecordsBean> list) {
        this.list.clear();
        addList(list);
    }

    public List<NewBaoLiaoListModel.DataBean.PageRecordsBean> getList() {
        return list;
    }

    public void addList(List<NewBaoLiaoListModel.DataBean.PageRecordsBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public NewBaoLiaoListModel.DataBean.PageRecordsBean getData(int position) {
        return list.get(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.my_baoliao_item, parent, false));
    }

    private String replaceString(String keyString) {
        return "<font color=\"" + AppFactoryGlobalConfig.getAppServerConfigInfo(context).getColor() + "\" font-size=\"16px\">" + keyString + "</font>";
    }

    @Override
    public int getItemViewType(int position) {
        //0：纯图片     1含有视频   2有视频有图片  3：无素材（纯文本）
        if (list.get(position).getMaterialType().equals("0")) {
            return 0;
        } else if (list.get(position).getMaterialType().equals("1")) {
            return 1;
        }  else if (list.get(position).getMaterialType().equals("2")) {
            return 2;
        } else  {
            return 3;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        NewBaoLiaoListModel.DataBean.PageRecordsBean meta = list.get(position);
        //2,”发布” 3：未发布 5：下线
        if (meta.getStatus() == 2) {
            holder.addTime.setText(meta.getPublishTime());
            holder.commentLayout.setVisibility(View.VISIBLE);
            holder.commentCount.setText(meta.getHitcount() + "");
        } else {
            holder.addTime.setText(meta.getAddTime());
            holder.commentLayout.setVisibility(View.GONE);
            Context context = holder.tvSHNpass.getContext();
            GradientDrawable gradientDrawable = new DrawableUtils.GradientDrawableBuilder(context, R.drawable.common_gradient_drawable)
                    .setRadius((int) context.getResources().getDimension(R.dimen.dimen2))
                    .setColor(AppFactoryGlobalConfig.getAppServerConfigInfo(context).getMainColor())
                    .create();
            holder.tvSHNpass.setBackground(gradientDrawable);
            holder.tvSHNpass.setVisibility(View.GONE);
            if (meta.getCheckStatus() == 3) {
                holder.tvSHNpass.setVisibility(View.VISIBLE);
                holder.tvSHNpass.setText("未通过");
            } else if (meta.getCheckStatus() == 1) {
                holder.tvSHNpass.setVisibility(View.VISIBLE);
                holder.tvSHNpass.setText("待审核");
            }
        }
        boolean isRound = AppFactoryGlobalConfig.getAppServerConfigInfo(context).isRound();

        if (getItemViewType(position) == 3 || meta.getStatus() == 5) {//原因，在审核失败的情况下如果设置不显示那么这个里面就只有文字
            holder.imageLayout.setVisibility(View.GONE);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.infoLayout.getLayoutParams();
            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
            holder.infoLayout.setLayoutParams(layoutParams);
        } else {
            holder.imageLayout.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.infoLayout.getLayoutParams();
            layoutParams.height = context.getResources().getDimensionPixelOffset(R.dimen.dimen90);
            holder.infoLayout.setLayoutParams(layoutParams);
            holder.image.defaultBackGroundResId = DefaultBgUtil.getDefaultImageBg(context);
            holder.image.defaultDrawable = AppFactoryGlobalConfig.getDefaultImageLoadDrawable();
            holder.image.setDefaultRes();
            List<NewBaoLiaoListModel.DataBean.PageRecordsBean.ShortTitleStyleBean> shortTitleStyle = meta.getShortTitleStyle();
            if (shortTitleStyle.size() > 0) {
                if(shortTitleStyle.get(0).getType().equals("image")){
                    holder.image.load(shortTitleStyle.get(0).getImgpath(), isRound);
                }else{
                    if (list.get(position).getTipoffinfoList() != null && list.get(position).getTipoffinfoList().size() > 0) {
                        if (list.get(position).getTipoffinfoList().get(0) != null && list.get(position).getTipoffinfoList().get(0).getLiveAddress().size() > 0) {
                            if (list.get(position).getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats() != null && list.get(position).getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().size() > 0) {
                                for (int i = 0; i < list.get(position).getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().size(); i++) {
                                    holder.image.load(list.get(position).getTipoffinfoList().get(0).getLiveAddress().get(0).getFormats().get(0).getImage(), isRound);
                                }
                            }
                        }
                    }
                }
            }
            if (list.get(position).getShortTitleStyle() != null && list.get(position).getShortTitleStyle().size() > 0) {
                if(list.get(position).getShortTitleStyle().get(0).getType().equals("image")){
                    holder.videoLogo.setVisibility(View.GONE);
                }else {

                    holder.videoLogo.setVisibility(View.VISIBLE);
                }
            }else {
                holder.videoLogo.setVisibility(View.GONE);
            }
        }


        setImageDirection(holder);
        holder.title.setText(meta.getContent());
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        listener.onClick(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout imageLayout;
        RelativeLayout infoLayout;
        LinearLayout commentLayout;
        NetImageViewX image;
        ImageView videoLogo;
        TextView title;
        TextView addTime;
        TextView commentCount;
        /**
         * 审核未通过
         */
        TextView tvSHNpass;

        public ViewHolder(View itemView) {
            super(itemView);
            imageLayout = itemView.findViewById(R.id.imageLayout);
            infoLayout = itemView.findViewById(R.id.infoLayout);
            commentLayout = itemView.findViewById(R.id.commentLayout);
            image = itemView.findViewById(R.id.image);
            videoLogo = itemView.findViewById(R.id.videoLogo);
            title = itemView.findViewById(R.id.title);
            addTime = itemView.findViewById(R.id.addTime);
            commentCount = itemView.findViewById(R.id.commentCount);
            tvSHNpass = itemView.findViewById(R.id.tv_sheng_he_no_pass);
        }
    }

    private void setImageDirection(ViewHolder holder) {
        boolean isLeftImgMode = ListStyle.TotoalStyle.LEFT_IMAGE.equals(AppFactoryGlobalConfig.getAppServerConfigInfo(context).getContent_list());
        // isLeftImgMode = true;
        if (isLeftImgMode) {
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) holder.imageLayout.getLayoutParams();
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
            layoutParams.setMarginStart(0);
            holder.imageLayout.setLayoutParams(layoutParams);

            RelativeLayout.LayoutParams layoutParams2 = (RelativeLayout.LayoutParams) holder.infoLayout.getLayoutParams();
            layoutParams2.removeRule(RelativeLayout.START_OF);
            layoutParams2.addRule(RelativeLayout.END_OF, holder.imageLayout.getId());
            int margin = context.getResources().getDimensionPixelOffset(R.dimen.dimen10);
            layoutParams2.setMarginStart(margin);
            holder.infoLayout.setLayoutParams(layoutParams2);
        }
    }


}